package com.zx.onlineeducation.service.edu.controller;


import com.zx.onlineeducation.common.util.R;
import com.zx.onlineeducation.service.edu.entity.ChapterDO;
import com.zx.onlineeducation.service.edu.entity.vo.ChapterTreeVO;
import com.zx.onlineeducation.service.edu.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 章节 前端控制器
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-13
 */
@Api(tags = "课程章节管理")
@RestController
@RequestMapping("/edu/chapter")
@CrossOrigin // 解决跨域
public class ChapterController {

    /**
     * 课程章节服务接口
     */
    @Autowired
    private ChapterService chapterService;

    @ApiOperation("课程章节树形结构列表")
    @GetMapping("/tree/{courseId}")
    public R tree(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable("courseId") String courseId) {
        List<ChapterTreeVO> tree =  chapterService.getTree(courseId);
        return R.ok().data("tree", tree);
    }

    @ApiOperation("添加课程章节")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "课程章节数据对象")
            @RequestBody ChapterDO chapterDO) {
        boolean save = chapterService.save(chapterDO);
        return save ? R.ok() : R.error();
    }

    @ApiOperation("根据ID查询课程章节")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(value = "课程章节ID", required = true)
            @PathVariable("id") String id) {
        ChapterDO chapterDO = chapterService.getById(id);
        return R.ok().data("chapter", chapterDO);
    }

    @ApiOperation("修改课程章节")
    @PutMapping("/{id}")
    public R update(
            @ApiParam(value = "课程章节ID", required = true)
            @PathVariable("id") String id,
            @ApiParam(value = "课程章节数据对象")
            @RequestBody ChapterDO chapterDO) {
        chapterDO.setId(id);
        boolean update = chapterService.updateById(chapterDO);
        return update ? R.ok() : R.error();
    }

    @ApiOperation("逻辑删除课程章节")
    @DeleteMapping("/{id}")
    public R remove(
            @ApiParam(value = "课程章节ID", required = true)
            @PathVariable("id") String id) {
        boolean remove = chapterService.removeChapterById(id);
        return remove ? R.ok() : R.error();
    }
}

